package com.keyinong.caizhengting.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.entity.MyteamEntity;
import com.keyinong.caizhengting.myfragement.TeamdetailActivity;

import java.util.ArrayList;

/**
 * 我的团队适配器
 */

public class MyteamAdapter extends BaseAdapter {

    Context context;
    ArrayList<MyteamEntity> myteamList;
    HttpTools httpTools = null;

    public MyteamAdapter(Context context, ArrayList<MyteamEntity> myteamList) {
        super();
        this.context = context;
        this.myteamList = myteamList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myteamList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return myteamList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHandle handle;
        if (view == null) {
            handle = new ViewHandle();
            view = LayoutInflater.from(context).inflate(R.layout.item_myteam, null);
            handle.tv_teamname = (TextView) view.findViewById(R.id.tv_teamname);
            handle.tv_cmp = (TextView) view.findViewById(R.id.tv_cmp);
            handle.tv_leader = (TextView) view.findViewById(R.id.tv_leader);
            handle.ll_xiangqing = (RelativeLayout) view.findViewById(R.id.ll_xiangqiang);
            view.setTag(handle);
        } else {
            handle = (ViewHandle) view.getTag();
        }
        handle.tv_teamname.setText(myteamList.get(position).getName());
        if (myteamList.get(position).getCname().equals("null")) {
            handle.tv_cmp.setText("");
        } else {
            handle.tv_cmp.setText(myteamList.get(position).getCname());
        }
        handle.tv_leader.setText(myteamList.get(position).getUname());
        handle.ll_xiangqing.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, TeamdetailActivity.class);
                intent.putExtra("teamid", myteamList.get(position).getId());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
        });
        return view;
    }

    class ViewHandle {
        /**
         * 团队名
         */
        TextView tv_teamname;
        /**
         * 公司名
         */
        TextView tv_cmp;
        /**
         * 负责人
         */
        TextView tv_leader;
        /**
         * 详情
         */
        RelativeLayout ll_xiangqing;
    }

}
